package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallOrderCommissionMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.db.vo.PageVO;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单佣金
 */
@Component
public class LitemallOrderCommissionService {
    @Resource
    private LitemallOrderCommissionMapper orderCommissionMapper;

    public List<LitemallOrderCommission> queryByUid(Integer uid) {
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false);
        return orderCommissionMapper.selectByExample(example);
    }

    public List<LitemallOrderCommission> queryByExample(PageVO<LitemallOrderCommission, LitemallOrderCommission> page) {
        if (page.getSize() == null) {
            page.setSize(10);
        }
        if (page.getPage() == null) {
            page.setPage(0);
        }
        if (page.getCondition() != null) {
        }
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        LitemallOrderCommissionExample.Criteria criteria = example.createCriteria();
        if (page.getBeginTime() != null) {
            criteria.andCreatedTimeGreaterThanOrEqualTo(page.getBeginTime());
        }
        if (page.getEndTime() != null) {
            criteria.andCreatedTimeLessThanOrEqualTo(page.getEndTime());
        }
        if (page.getCondition() != null) {
            LitemallOrderCommission condition = page.getCondition();
            if (condition.getUserId() != null) {
                criteria.andUserIdEqualTo(condition.getUserId());
            }
            if (condition.getUsername() != null) {
                criteria.andUsernameLike(condition.getUsername());
            }
            if (condition.getRecommenderId() != null) {
                criteria.andRecommenderIdEqualTo(condition.getRecommenderId());
            }
            if (condition.getOrderStatus() != null) {
                criteria.andOrderStatusEqualTo(condition.getOrderStatus());
            }
        }
        if (!StringUtils.isEmpty(page.getSort()) && !StringUtils.isEmpty(page.getOrder())) {
            example.setOrderByClause(page.getSort() + " " + page.getOrder());
        }
        PageHelper.startPage(page.getPage(), page.getSize());
        List<LitemallOrderCommission> list = orderCommissionMapper.selectByExample(example);
        if(list != null) {
            for(LitemallOrderCommission commission : list) {
                LitemallOrder order = new LitemallOrder();
                order.setOrderStatus(Short.valueOf(commission.getOrderStatus()));
                commission.setOrderStatus(OrderUtil.orderStatusText(order));
            }
        }
        return list;
    }

    public LitemallOrderCommission query(Integer userId, Integer id) {
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return orderCommissionMapper.selectOneByExample(example);
    }

    public int add(LitemallOrderCommission orderCommission) {
        orderCommission.setCreatedTime(LocalDateTime.now());
        return orderCommissionMapper.insert(orderCommission);
    }

    public int update(LitemallOrderCommission orderCommission) {
        orderCommission.setCreatedTime(LocalDateTime.now());
        return orderCommissionMapper.updateByPrimaryKeySelective(orderCommission);
    }

    public void delete(Integer id) {
        orderCommissionMapper.logicalDeleteByPrimaryKey(id);
    }

    public LitemallOrderCommission findDefault(Integer userId) {
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        example.or().andUserIdEqualTo(userId).andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        return orderCommissionMapper.selectOneByExample(example);
    }

    public void resetDefault(Integer userId) {
        LitemallOrderCommission orderCommission = new LitemallOrderCommission();
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        orderCommissionMapper.updateByExampleSelective(orderCommission, example);
    }

    public List<LitemallOrderCommission> querySelective(Integer userId, String username, Integer recommenderId, String recommendername, Integer orderId, String orderSn, Integer page, Integer limit, String sort, String order) {
        LitemallOrderCommissionExample example = new LitemallOrderCommissionExample();
        LitemallOrderCommissionExample.Criteria criteria = example.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (recommenderId != null) {
            criteria.andRecommenderIdEqualTo(recommenderId);
        }
        if (!StringUtils.isEmpty(recommendername)) {
            criteria.andRecommendernameLike("%" + recommendername + "%");
        }
        if (orderId != null) {
            criteria.andOrderIdEqualTo(orderId);
        }
        if (!StringUtils.isEmpty(orderSn)) {
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return orderCommissionMapper.selectByExample(example);
    }
}
